<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Customer extends Model
{
    use SoftDeletes, HasDateTimeFormatter;


    protected $guarded = [];


    //接待部人
    public function receptionUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'reception_user_id')->withTrashed();
    }

    //销售部人
    public function saleUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'sale_user_id')->withTrashed();
    }

    //复诊部人
    public function returnUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'return_user_id')->withTrashed();
    }

    //来源
    public function source(): BelongsTo
    {
        return $this->belongsTo(Option::class, 'source_option_id')->withTrashed();
    }



    //医生
    public function doctor(): BelongsTo
    {
        return $this->belongsTo(Option::class, 'doctor_option_id')->withTrashed();
    }

    //销售业绩
    public function salePerformance(): HasMany
    {
        return $this->hasMany(SalePerformance::class);
    }

    //销售业绩第一条业绩
    public function salePerformanceFirst(): HasOne
    {
        return $this->hasOne(SalePerformance::class)->orderBy('id');
    }


    // 定义一个public方法访问图片或文件
    public function getChatImages()
    {
        if (!$this->chat_images) {
            return $this->chat_images;
        }
        $images = [];
        foreach (json_decode($this->chat_images, true) as $item) {
            $images[] = getAliOssUrl($item);
        }
        return $images;
    }


    //公海
//    public function saleOpen(): HasOne
//    {
//        return $this->hasOne(SaleOpen::class);
//    }

}
